# import socket
# import time
#
# import serial
#
# if __name__ == '__main__':
#     # 打开串口
#     serial_port = serial.Serial('COM13', 115200, timeout=5)  ##连接串口，打开
#
#     while True:
#         # 创建tcp
#         # 1.创建socket
#         tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#         # 2. 链接服务器
#         server_addr = ("127.0.0.1", 1688)
#         tcp_socket.connect(server_addr)
#
#         # 读取串口数据发送到tcp
#         Read = serial_port.read_all()  ###读多行数据
#         if(Read !=b''):
#             print(Read)
#             tcp_socket.send(str(Read).encode("gbk"))
#
#         # 接收tcp数据发送到串口
#         receive = tcp_socket.recv(2048)
#         print(receive.decode('gbk'))
#         Write = serial_port.write(receive)  ##发送数据
#         time.sleep(0.1)
#         # 4. 关闭套接字
#         tcp_socket.close()
#
#     serial_port.close()  ###关闭串口连接


# -*- coding: utf-8 -*-
import asyncio
from bleak import BleakClient, BleakScanner
from bleak.backends.characteristic import BleakGATTCharacteristic

# #设备的Characteristic UUID
# par_notification_characteristic="0000ffe1-0000-1000-8000-00805f9b34fb"
# #设备的MAC地址
# par_device_addr="88:25:83:F3:86:C1"

#监听回调函数，此处为打印消息
# def notification_handler(characteristic: BleakGATTCharacteristic, data: bytearray):
#     print("rev data:",data)

async def main():
    print("starting scan...")

    #基于MAC地址查找设备
    device = await BleakScanner.find_device_by_name("VDB1615")
    # device = await BleakScanner.find_device_by_address(
    #     par_device_addr, cb=dict(use_bdaddr=False)  #use_bdaddr判断是否是MOC系统
    # )
    if device is None:
        print("could not find device with address")
        return
    print(device.address)
    print(device.rssi)
    print(device.details)
    # print(device.)
    print("connecting to device...")
    # async with BleakClient(device) as client:
    #     print("Connected")
    #     await client.start_notify(par_notification_characteristic, notification_handler)
    #     await asyncio.sleep(10.0)   #程序监听的时间，此处为10秒
    #     await client.stop_notify(par_notification_characteristic)

asyncio.run(main())

